<?php

namespace Fize\Third\Dify;

/**
 * 知识库
 */
class Datasets extends ApiAbstract
{

    /**
     * 创建空知识库
     * @param string      $name                      知识库名称
     * @param string|null $description               知识库描述
     * @param string|null $indexing_technique        索引模式
     * @param string|null $permission                权限
     * @param string|null $provider                  Provider
     * @param string|null $external_knowledge_api_id 外部知识库API_ID
     * @param string|null $external_knowledge_id     外部知识库ID
     * @param string|null $embedding_model           Embedding模型名称
     * @param string|null $embedding_model_provider  Embedding模型供应商
     * @param array|null  $retrieval_model           检索模式
     * @return array
     */
    public function create(
        string $name, string $description = null, string $indexing_technique = null, string $permission = null, string $provider = null,
        string $external_knowledge_api_id = null, string $external_knowledge_id = null,
        string $embedding_model = null, string $embedding_model_provider = null, array $retrieval_model = null
    ): array
    {
        $params = [
            'name' => $name
        ];
        if (!is_null($description)) {
            $params['description'] = $description;
        }
        if (!is_null($indexing_technique)) {
            $params['indexing_technique'] = $indexing_technique;
        }
        if (!is_null($permission)) {
            $params['permission'] = $permission;
        }
        if (!is_null($provider)) {
            $params['provider'] = $provider;
        }
        if (!is_null($external_knowledge_api_id)) {
            $params['external_knowledge_api_id'] = $external_knowledge_api_id;
        }
        if (!is_null($external_knowledge_id)) {
            $params['external_knowledge_id'] = $external_knowledge_id;
        }
        if (!is_null($embedding_model)) {
            $params['embedding_model'] = $embedding_model;
        }
        if (!is_null($embedding_model_provider)) {
            $params['embedding_model_provider'] = $embedding_model_provider;
        }
        if (!is_null($retrieval_model)) {
            $params['retrieval_model'] = $retrieval_model;
        }
        return $this->httpPost('/datasets', $params);
    }

    /**
     * @param int|null    $page
     * @param int|null    $limit
     * @param string|null $keyword
     * @param null        $tag_ids
     * @param null        $include_all
     * @return array
     */
    public function getList(int $page = null, int $limit = null, string $keyword = null, $tag_ids = null, $include_all = null): array
    {
        $params = [];
        if (!is_null($page)) {
            $params[] = "page={$page}";
        }
        if (!is_null($limit)) {
            $params[] = "limit={$limit}";
        }
        if (!is_null($keyword)) {
            $params[] = "keyword={$keyword}";
        }
        if (!is_null($tag_ids)) {
            $params[] = "tag_ids={$tag_ids}";
        }
        if (!is_null($include_all)) {
            $params[] = "include_all={$include_all}";
        }
        $uri = '/datasets';
        if (!empty($params)) {
            $uri .= '?' . implode('&', $params);
        }
        return $this->httpGet($uri);
    }

    /**
     * 查看知识库详情
     * @param string $dataset_id 知识库ID
     * @return array
     */
    public function getDetail(string $dataset_id): array
    {
        $uri = "/datasets/{$dataset_id}";
        return $this->httpGet($uri);
    }

    /**
     * 删除知识库
     * @param string $dataset_id 知识库ID
     * @return array
     */
    public function delete(string $dataset_id): array
    {
        $uri = "/datasets/{$dataset_id}";
        return $this->httpDelete($uri);
    }
}